package com.stone.framework.user.service.impl;

import java.util.List;
import java.util.stream.Collectors;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.stone.framework.core.exception.BusinessException;
import com.stone.framework.core.util.Beans;
import com.stone.framework.message.user.DeptReqBody;
import com.stone.framework.message.user.DeptTreeResBody;
import com.stone.framework.user.entity.Dept;
import com.stone.framework.user.entity.User;
import com.stone.framework.user.mapper.DeptMapper;
import com.stone.framework.user.mapper.UserMapper;
import com.stone.framework.user.service.DeptService;

@Service
@Transactional(readOnly = true)
public class DeptServiceImpl implements DeptService {

    @Autowired
    private DeptMapper deptMapper;

    @Autowired
    private UserMapper userMapper;

    @Override
    public List<DeptTreeResBody> buildTree() {
        List<Dept> list = this.queryAll();
        return list.stream().filter(dept -> dept.getParentId().equals(0L)).map(dept -> this.doBuildTree(dept, list))
            .collect(Collectors.toList());
    }

    // @Override
    // public String queryByUsername(String username) {
    // return deptMapper.selectByUsername(username);
    // }

    @Override
    @Transactional(readOnly = false)
    public void save(DeptReqBody reqBody) {
        Dept entity = new Dept();
        Beans.copyProperties(reqBody, entity);
        Integer count = deptMapper.selectCount(new QueryWrapper<Dept>().eq("code", entity.getCode()));
        if (count > 0) {
            throw new BusinessException("部门已存在，请重新输入");
        }
        deptMapper.insert(entity);
    }

    @Override
    @Transactional(readOnly = false)
    public void modify(DeptReqBody reqBody) {
        Dept entity = deptMapper.selectById(reqBody.getId());
        if (entity == null) {
            throw new BusinessException("部门不存在");
        }
        if (!entity.getCode().equals(reqBody.getCode())) {
            throw new BusinessException("部门代码不可修改");
        }

        Beans.copyProperties(reqBody, entity);
        deptMapper.updateById(entity);
    }

    @Override
    @Transactional(readOnly = false)
    public void delete(Long id) {
        Integer count = deptMapper.selectCount(new QueryWrapper<Dept>().eq("parent_id", id));
        if (count > 0) {
            throw new BusinessException("请先删除子节点");
        }

        Dept entity = deptMapper.selectById(id);
        if (entity == null) {
            throw new BusinessException("部门不存在");
        }

        Integer relationCount = userMapper.selectCount(new QueryWrapper<User>().eq("dept_code", entity.getCode()));
        if (relationCount > 0) {
            throw new BusinessException("请先删除该部门下相关人员");
        }
        deptMapper.deleteById(id);
    }

    private List<Dept> queryAll() {
        return deptMapper.selectList(null);
    }

    private DeptTreeResBody doBuildTree(Dept root, List<Dept> list) {
        List<DeptTreeResBody> children =
            list.stream().filter(node -> root.getId().equals(node.getParentId())).map(node -> {
                DeptTreeResBody child = this.doBuildTree(node, list);
                Beans.copyProperties(node, child);
                return child;
            }).collect(Collectors.toList());

        DeptTreeResBody resBody = new DeptTreeResBody();
        Beans.copyProperties(root, resBody);
        resBody.setChildren(children);
        return resBody;
    }

}
